package com.hunlian.makelove.tools;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.hunlian.makelove.common.Printout;
import com.hunlian.makelove.net.NetTools;
import com.hunlian.makelove.view.ShowMsgPopupWindow;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 设备工具类
 * 
 * @author forcetech
 * 
 */
public class DeviceTools {
	/**
	 * 获得设备系统
	 * 
	 * @return
	 */
	public static String getDeviceOs() {
		return "android/" + android.os.Build.VERSION.RELEASE;
	}

	/**
	 * 获得设备型号
	 * 
	 * @return
	 */
	public static String getDeviceModel() {
		return android.os.Build.MANUFACTURER + "/" + android.os.Build.MODEL;
	}

	/**
	 * 获得设备系统语言
	 * 
	 * @param context
	 * @return
	 */
	public static String getDeviceLanguage(Context context) {
		return context.getResources().getConfiguration().locale
				.getDisplayName()
				+ "/"
				+ context.getResources().getConfiguration().locale
						.getLanguage();
	}

	/**
	 * 获得设备网络类型
	 * 
	 * @param context
	 * @return
	 */
	public static String getDeviceNetworkType(Context context) {
		return NetTools.getNetworkTypeName(context);
	}

	/**
	 * 获得屏幕设备信息
	 * 
	 * @param context
	 * @return
	 */
	public static DisplayMetrics getDisplayMetrics(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		manager.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics;
	}

	public static String getCurrentTime(String format) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
		String currentTime = sdf.format(date);
		return currentTime;
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		return getCurrentTime("yyyy-MM-dd  HH:mm:ss");
	}
	
	/**
	 * 获取当前时间的时间戳
	 */
	public static long getStringDayDatess(){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		Date date = new Date();
		try {
			date = sf.parse(getCurrentTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date.getTime();
	}
	/**
	 * 获取当前时间的时间戳
	 */
	public static String getCalendar(Date date){
		SimpleDateFormat sf = new SimpleDateFormat("MM月dd日");
		String sDate = sf.format(date);

		return sDate;
	}
	
	/**
	 * 获取当前时间的时间戳
	 */
	public static String getYearMonth(Date date){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月");
		String sDate = sf.format(date);

		return sDate;
	}
	/**
	 * 获取当前时间  2015-06-16
	 */
	public static String getYearMonthDay(){
		String date = getCurrentTime("yyyy-MM-dd");
		return date;
	}
	/**
	 * 获取以2015-06-16为格式的时间戳
	 */
	public static long getYearMonthDayNum(String time){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try {
			date = sf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date.getTime();
	}
	/**
	 * 获取当前日期时间戳
	 */
	public static long getYearMothDayNum(){
		return getYearMonthDayNum(getYearMonthDay());
	}
	/**
	 * 判断是否符合精度为小数点后两位的数
	 */
	public static boolean isAssetsNo(String mobiles,Activity context,EditText edit){
		Printout.println("mobiles:"+mobiles);
		if(mobiles.contains(".")&&mobiles.length()!=1){
			if(mobiles.indexOf(".")==0){
				edit.setText(mobiles.substring(1, mobiles.length()));
				//edit.setSelection(mobiles.substring(1, mobiles.length()).length());
				ShowMsgPopupWindow.showText(context, "第一位不可为.");
				return true;
			}
			if(mobiles.indexOf(".")==mobiles.length()-1){
				if(mobiles.length()>9){
					edit.setText(mobiles.substring(0, mobiles.length()-1));
					edit.setSelection(mobiles.substring(0, mobiles.length()-1).length());
					ShowMsgPopupWindow.showText(context, "投资金额最大不超过千万级");
				}
				return true;
			}else{
				Pattern p = Pattern.compile("^[0-9]+([.]{1}[0-9]{1,2})?$");
				Matcher m = p.matcher(mobiles);
				if(!m.matches()){
					int i = mobiles.indexOf(".");
					if(i>mobiles.length()-5){
						edit.setText(mobiles.substring(0, mobiles.length()-1));
						edit.setSelection(mobiles.substring(0, mobiles.length()-1).length());
						ShowMsgPopupWindow.showText(context, "只能精确到小数点后两位");
						Printout.println(mobiles.substring(0, mobiles.length()-1));
					}else{
						mobiles = mobiles.substring(0, i)+mobiles.substring(i+1);
						edit.setText(mobiles);
						edit.setSelection(mobiles.length());
						ShowMsgPopupWindow.showText(context, "只能精确到小数点后两位");
					}
				}else{
					String[] sp = mobiles.split("\\.");
					Printout.println("个数："+sp.length);
					if(sp[0].length()>8){
						String l = sp[0].substring(0, sp[0].length()-1)+"."+sp[1];
						edit.setText(l);
						edit.setSelection(l.length());
						ShowMsgPopupWindow.showText(context, "投资金额最大不超过千万级");
						return false;
					}else{
						return true;
					}
				}
				return m.matches();
			}
		}else if(mobiles.contains(".")&&mobiles.length()==1){
			edit.setText("");
			ShowMsgPopupWindow.showText(context, "第一位不可为.");
			return false;
		}else if(mobiles.length()>8){

				mobiles = mobiles.substring(0, 8);
				edit.setText(mobiles);
				edit.setSelection(mobiles.length());
				ShowMsgPopupWindow.showText(context, "投资金额最大不超过千万级");
				return false;
			//}
		}else{
			return true;
		}
	}

	/**
	 * 判断是否有表情符号
	 */
	public static boolean ifHaveBq(String mobiles){
		if(TextUtils.isEmpty(mobiles)){
			return false;
		}
		Pattern p = Pattern.compile("^[A-Za-z0-9\u4e00-\u9fa5]+$");
		Matcher m = p.matcher(mobiles);
		if(!m.matches()){
			return true;
		}
		return false;
	}
	/**
	 * 搜索框输入规则校验
	 */
	public static boolean SearchCheck(String mobiles,Activity context){
		if(TextUtils.isEmpty(mobiles)){
			ShowMsgPopupWindow.showText(context, "请输入搜索内容");
			return false;
		}
		Pattern p = Pattern.compile("^[A-Za-z0-9\u4e00-\u9fa5]+$");
		Matcher m = p.matcher(mobiles);
		if(!m.matches()){
			ShowMsgPopupWindow.showText(context, "亲，只能输入字母、数字或汉字喔！");
			return false;
		}
		return true;
	}
	/**
	 * 控制软件盘弹出
	 * @param editText
	 */
	public static void openInputMethod(final EditText editText) {

		Timer timer = new Timer();

		timer.schedule(new TimerTask() {

			public void run() {

				InputMethodManager inputManager = (InputMethodManager) editText

				.getContext().getSystemService(

				Context.INPUT_METHOD_SERVICE);

				inputManager.showSoftInput(editText, 0);

			}

		}, 200);

	}
	
	/**
	 * 转换成日期
	 * 
	 * @param time
	 * @return yyyy-MM-dd
	 */
	public static String formatTimeToDate(long time) {
		return formatTimeToFormat("yyyy-MM-dd", time);
	}
	
	/**
	 * 时间转换
	 * 
	 * @param format
	 * @param time
	 * @return
	 */
	public static String formatTimeToFormat(String format, long time) {
		if (TextUtils.isEmpty(format))
			return String.valueOf(time);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (String.valueOf(time).length() < 13) {
			return sdf.format(time * 1000l);
		} else {
			return sdf.format(time);
		}
	}

	/**
	 * 释放图片
	 */
	public static void releaseBitmap(Bitmap bitmap) {
		if (bitmap != null) {
			try {
				if (!bitmap.isRecycled()) {
					bitmap.recycle();
				}
			} catch (Exception e) {
			}

		}
	}

}
